package com.wenhai.leetcode.num;

/**
 * https://leetcode-cn.com/problems/reverse-integer/
 *
 * @author wenhai
 * @date   2021/3/18
 */
public class _7整数反转 {
    public int reverse(int x) {
        String numStr = String.valueOf(x);
        boolean isNegative = false;
        if (numStr.startsWith("-")) {
            isNegative = true;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = numStr.toCharArray().length - 1; i >= 0; i--) {
            sb.append(numStr.toCharArray()[i]);
        }

        if (isNegative) {
            sb.deleteCharAt(sb.length() - 1).insert(0, '-');
        }
        try {
            return Integer.valueOf(sb.toString());
        } catch (NumberFormatException e) {
            return 0;
        }
    }
    public int reverse2(int x) {

        return Integer.reverse(x);

    }

    public static void main(String[] args) {
        System.out.println(new _7整数反转().reverse2(Integer.MAX_VALUE));
    }
}
